import Taro from "@tarojs/taro";
import { BaseApi, ClientOptions } from "./flora-api-base";

export type GuestUserInfo = { status: 'guest' }

export type UserInfo = {
  uid: number,
  email: string,
  name: string,
  is_dev: boolean,
};

export function createUsersApi(opts: { clientOptions: ClientOptions, base: BaseApi }) {
  const request = opts.base.request;
  const base = opts.base;
  // 老师账号密码登录登录，并自动设置login token用于后续查询。
  async function login(opts: { account: string, password: string }): Promise<{ status: 'ok' } | { status: 'error', errorMsg: string }> {
    const r = await request<{ status: 'ok', token: string } | { status: 'error', errorMsg: string, subKind: string }>({
      // url: '/miniapp/account_login',
      url:'/api/auth/v1/request-login-token',
      method: 'POST',
      data: {
        account: opts.account,
        password: opts.password,
      },
      header: {
        // 暂不携带access token.
        Authorization: '',
      }
    });
    if ('data' in r) {
      if (r.data.status === 'ok') {
        const token = r.data.token;
        await base.setLoginToken(token);
        return { status: 'ok' };
      }
      return r.data;
    }
    console.log('login error:', r);
    return { status: 'error', errorMsg: '请求失败' };
  }

    // 学生密码登录登录，并自动设置login token用于后续查询。
  async function studentlogin(opts: { code: string, name: string,idcard:string,logincode:string }): Promise<{ status: 'ok' } | { status: 'error', errorMsg: string }> {
      const r = await request<{ status: 'ok', token: string,type: String,personid: String } | { status: 'error', errorMsg: string, subKind: string }>({
        // url: '/miniapp/account_login',
        url:'/miniapp/studentLogin',
        method: 'POST',
        data: {
          code: opts.code,
          name: opts.name,
          id_card:opts.idcard,
          login_code:opts.logincode,
        },
        header: {
          // 暂不携带access token.
          Authorization: '',
        }
      });
      if ('data' in r) {
        if (r.data.status === 'ok') {
          console.log("++++++",r)
          Taro.setStorageSync("personid",r.data.personid) 
          Taro.setStorageSync("type",r.data.type)           
          const token = r.data.token;
          await base.setLoginToken(token);
          return { status: 'ok' };
        }
        return r.data;
      }
      console.log('login error:', r);
      return { status: 'error', errorMsg: '请求失败' };
    }
  
  // OPenid登录，并自动设置login token用于后续查询。
  async function OpenIdlogin(opts: {openid: string }): Promise<{ status: 'ok' } | { status: 'error', errorMsg: string }> {
    const r = await request<{ status: 'ok', token: string } | { status: 'error', errorMsg: string, subKind: string }>({
      // url: '/miniapp/account_login',
      url:'/miniapp/openid_login',
      method: 'POST',
      data: {
        code: opts.openid,
      },
      header: {
        // 暂不携带access token.
        Authorization: '',
      }
    });
    if ('data' in r) {
      if (r.data.status === 'ok') {
        Taro.setStorageSync("personid",r.data.personid) 
        Taro.setStorageSync("type",r.data.type) 
        const token = r.data.token;
        await base.setLoginToken(token);
        return { status: 'ok' };
      }
      return r.data;
    }
    console.log('login error:', r);
    return { status: 'error', errorMsg: '请求失败' };
  }

   // 学生密码登录登录，并自动设置login token用于后续查询。
   async function teacherLogin(opts: { email: string, pwd: string,logincode:string }): Promise<{ status: 'ok' } | { status: 'error', errorMsg: string }> {
    const r = await request<{ status: 'ok', token: string,type: String,personid: String } | { status: 'error', errorMsg: string, subKind: string }>({
      // url: '/miniapp/account_login',
      url:'/miniapp/teacherLogin',
      method: 'POST',
      data: {
        email: opts.email,
        pwd: opts.pwd,
        login_code:opts.logincode,
      },
      header: {
        // 暂不携带access token.
        Authorization: '',
      }
    });
    if ('data' in r) {
      if (r.data.status === 'ok') {
        console.log("++++++",r)
        Taro.setStorageSync("personid",r.data.personid) 
        Taro.setStorageSync("type",r.data.type)           
        const token = r.data.token;
        await base.setLoginToken(token);
        return { status: 'ok' };
      }
      return r.data;
    }
    console.log('login error:', r);
    return { status: 'error', errorMsg: '请求失败' };
  }
  
  // OPenid登录，并自动设置login token用于后续查询。
  async function UpdateFile(opts: {file: string }): Promise<{ status: 'ok',imgid:string } | { status: 'error', errorMsg: string }> {
    console.log('opts.file-=--------:', opts.file);
    
    const r = await request<{ status: 'ok', id: string } | { status: 'error', errorMsg: string, subKind: string }>({
        // url: '/miniapp/account_login',
        url:'/blobs/fileupload',
        method: 'POST',
        data: {
          file: opts.file,
          public: 'true'
        },
        header: {
          // 暂不携带access token
          Authorization: '',
        }
      });
      if ('data' in r) {
        console.log('file data========:',r);
        if (r.data.status === 'ok') {
          console.log("最初打印数据源++++++",r)
          const id = r.data.id;
          return { status: 'ok',imgid:id};
        }
        return r.data;
      }
      console.log('login error:', r);
      return { status: 'error', errorMsg: '请求失败' };
    }



     // OPenid登录，并自动设置login token用于后续查询。
  async function UpdateStudentZd(opts: {id:Int32Array,zdtjs:Int32Array}): Promise<{ status: 'ok'} | { status: 'error', errorMsg: string }> {
    const r = await request<{ status: 'ok'} | { status: 'error', errorMsg: string, subKind: string }>({
      // url: '/miniapp/account_login',
      url:'/searchGet/studentUpdateZd',
      method: 'PUT',
      data: {
        id: opts.id,
        zdtjs:opts.zdtjs
      },
      header: {
        // 暂不携带access token.
        Authorization:'',
      }
    });
    if ('data' in r) {
      console.log('login error:', r);
      if (r.data.status === 'ok') {
        return { status: 'ok'};
      }
      return r.data;
    }
    console.log('login error:', r);
    return { status: 'error', errorMsg: '请求失败' };
  }


      // OPenid登录，并自动设置login token用于后续查询。
      async function getKanbanDate(opts: {}): Promise<{ status: 'ok',date:{}} | { status: 'error', errorMsg: string }> {
        const r = await request<{ status: 'ok',date:{},} | { status: 'error', errorMsg: string, subKind: string }>({
          // url: '/miniapp/account_login',
          url:'/searchGet/getKanbanDate',
          method: 'POST',
          data: {
           
          },
          header: {
            // 暂不携带access token.
            Authorization:'',
          }
        });
        if ('data' in r) {
          console.log('login error:', r);
          if (r.data.status === 'ok') {
            return { status: 'ok',date:r.data};
          }
          return r.data;
        }
        console.log('login error:', r);
        return { status: 'error', errorMsg: '请求失败' };
      }

  
  // 获取当前用户信息，这也是一个简单的调用自定义接口的例子。
  const userInfo = () => {
    return request<GuestUserInfo | { data: UserInfo }>({ url: '/user/info', method: 'POST' });
  };


  

  return { login, studentlogin,OpenIdlogin,UpdateFile,teacherLogin,userInfo,UpdateStudentZd,getKanbanDate};
};
